package studentinformationmanagementsystem.dao.impl;

import studentinformationmanagementsystem.dao.BaseDao;
import studentinformationmanagementsystem.dao.MajorDao;
import studentinformationmanagementsystem.entity.Department;
import studentinformationmanagementsystem.entity.Major;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class MajorDaolmpl extends BaseDao implements MajorDao {



    /**
     * 对专业信息进行添加
     * @param major  专业对象
     * @return  受影响的记录行数
     */
    @Override
    public int insert(Major major) {
        String sql = "INSERT INTO `t_major` (`major`, `majorction`) VALUES (?, ?)";
        return super.update(sql,major.getMajor(),major.getMajorction());
    }


    /**
     *  根据ID更新专业信息
     * @param major  专业对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Major major) {

        String sql = "UPDATE `t_major` SET `major` = ? , `majorction` = ? WHERE `majorid` = ? ";
        return super.update(sql,major.getMajor(),major.getMajorction(),major.getMajorid());

    }



    /**
     *  根据ID删除专业
     * @param major 专业ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Major major) {
        String sql = "DELETE FROM `t_major` WHERE `majorid` = ?";
        return super.update(sql, major.getMajorid());
    }




    /**
     * 查询所有专业信息
     * @return 所有专业信息
     */
    @Override
    public List<Major> queryAll() {
        String sql = "SELECT * FROM `t_major`";
        return super.queryForList(Major.class, sql);
    }



    /**
     * 根据专业ID查询专业信息
     * @param major 专业ID
     * @return 专业信息
     */
    @Override
    public Major queryById(Major major) {
        String sql = "SELECT * FROM `t_major` WHERE `majorid` = ?";
        return super.queryForOne(Major.class, sql, major.getMajorid());
    }


    /**
     * 根据专业名称查询专业信息
     * @param major 专业名称
     * @return 专业信息
     */
    @Override
    public Major queryBymajorNname(Major major) {
        String sql = "SELECT * FROM `t_major` WHERE `major` = ?";
        return super.queryForOne(Major.class, sql, major.getMajor());
    }


    /**
     * 查询总记录条数
     */
    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `t_major`";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    /**
     * 分页查询DAO层方法
     *
     * @param begin    起始记录行数
     * @param pageSize 每页展示数量
     * @return
     */
    @Override
    public List<Major> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `t_major` LIMIT ?, ?";
        return super.queryForList(Major.class, sql, begin, pageSize);
    }

}
